/**
 * 服务器处理客户端的数据任务
 *
 * @author bian
 * @createDate 2016.9.8
 */
var db = require("../models/index");
var Data = db.models.Data;

var dataTask = (function () {

    var setAirState = (state, deviceId) => {
        var p;
        p = new Promise(function (resolve, reject) {
            Data.update({
                value: state.value
            },{
                where : {
                    equipment : "intelAir",
                    type : state.type,
                    DeviceID : deviceId
                }
            }).then(function () {
                resolve(true);
            });
        });
        return p;
    };

    var setBodyState = (state, deviceId) => {
        var p;
        p = new Promise(function (resolve, reject) {
            Data.update({
                value : state.value
            },{
                where : {
                    equipment : "intelBody",
                    type : state.type,
                    DeviceID : deviceId
                }
            }).then(function(){
                resolve(true);
            });
        });
        return p;
    };

    var storeSensorData = (data, deviceId) => {
        var p;
        p = new Promise(function (resolve, reject) {
            Data.create({
                value: data.value,
                type : data.type,
                equipment : "intelSensor",
                DeviceID : deviceId
            }).then(function(data){
                resolve(data);
            })
        });
        return p;
    };

    var storeDoorBellData = (data, deviceId) => {
        var p;
        p = new Promise(function (resolve, reject) {
            Data.create({
                value : data.value,
                type : data.type,
                equipment : "intelDoorBell",
                DeviceID : deviceId
            }).then(function(data){
                resolve(data);
            });
        });
        return p;
    };

    var setPatchState = (state, deviceId) => {
        var p;
        p = new Promise(function (resolve, reject) {
            Data.update({
                value : state.value
            },{
                where : {
                    equipment : "intelPatch",
                    type : state.type,
                    DeviceID : deviceId
                }
            }).then(function(){
                resolve(true);
            });
        });
        return p;
    };

    return {
        setAirState,
        setBodyState,
        storeSensorData,
        storeDoorBellData,
        setPatchState
    };
}());

module.exports = dataTask;